<template>
	<view class="authLogin">
		<view class="pattern left">
			<image src="/static/authlogin/download_pattern_left.png" mode=""></image>
		</view>
		<view class="pattern right">
			<image src="/static/authlogin/download_pattern_right.png" mode=""></image>
		</view>
		<view class="main">
			<view class="icon">
				<image src="/static/temp/logo.jpg" mode=""></image>
			</view>
			<view class="name">
				纳芈汇
			</view>
			<!-- <hr style="width:400upx;margin: 0 auto;"> -->
			<button class="btn" open-type="getUserInfo" lang="zh_CN" @getuserinfo="getUserInfo" withCredentials="true">
				<i class="iconfont icon-weixin" style="display: inline-block;font-size: 34upx;"></i>
				微信授权登录
			</button>
			<button class="btn" open-type="getPhoneNumber" @getphonenumber="getPhoneNumber">
				获取手机号
			</button>
			<view class="btn" @click="cancel()">
				取消
			</view>
		</view>
	</view>
</template>

<script>
	import {mapState,mapMutations} from 'vuex';
	export default {
		data() {
			return {
				canIUse: wx.canIUse('button.open-type.getUserInfo'),
				userInfo: {},
				code: '',
				iv:'',
				encryptedData:''
			};
		},
		computed: {
			...mapState(['shareId','hasbingMobile'])
		},
		onLoad(options){
			uni.showLoading({
				title: 'loading'
			});
			if (options.shareId){
				this.shareId = options.shareId;
			}
			let _this = this;
			uni.login({
				provider: 'weixin',
				success: function(loginRes) {
					_this.code = loginRes.code;																																				
					uni.hideLoading();					
				},
			});
		},
		methods:{
			...mapMutations(['login']),			
			  getPhoneNumber:function(e) {
			    if(e.detail.errMsg=='getPhoneNumber:fail'){
					this.$api.msg('未获取到手机号码');
					return false;
				}else if(e.detail.iv==undefined||!e.detail.iv){
					this.$api.msg('授权失败');
					return false;
				}else{
					this.iv=e.detail.iv;
					this.encryptedData=e.detail.encryptedData;
					this.updateUserInfo();
				}
			  },		
			getUserInfo: function(e) {
				let _this = this;
				uni.getUserInfo({
					provider: 'weixin',
					success: function(infoRes) {
						_this.userInfo = infoRes.userInfo;
						_this.updateUserInfo();
					},
					fail(res) {}
				});
			},
			updateUserInfo() {
				if(!this.iv&&!this.encryptedData){
					this.$api.msg('未获取手机号');
					return false;
				}
				if(!this.userInfo){
					this.$api.msg('未授权登录');
					return false;
				}
				let _this = this;
				console.log(_this.shareId)
				uni.request({
					url: _this.$base_url + '/member/weChat',
					data: {
						user: JSON.stringify(_this.userInfo),
						referee_id: _this.shareId,
						code: _this.code,
						iv:_this.iv,
						encryptedData:_this.encryptedData
					},
					method: 'POST',
					header: {
						'content-type': 'application/x-www-form-urlencoded'
					},
					success: (res) => {
						if (res.data.code == 10000) {							
							let userInfo = _this.userInfo;
							userInfo.token = res.data.data.token;							
							let olduserInfo = uni.getStorageSync('userInfo');
							if (olduserInfo.mobile) {
								userInfo.mobile = olduserInfo.mobile
							}
							_this.login(userInfo);
							setTimeout(()=>{
								this.memberAccount();
							},300)																					
						} else {
							_this.$api.msg(res.data.message);
						}
			
					},
					fali: (res) => {
						console.log(res.data);
					}
				});
			},			
			memberAccount() {
				let userInfo = uni.getStorageSync('userInfo');
				uni.request({
					url: this.$base_url + '/member/memberInfo',
					data: {},
					header: {
						"Content-Type": "application/x-www-form-urlencoded",
						"token": userInfo.token,
					},
					method: "POST",
					success: (res) => {
						if (res.data.code == 10000) {
							let userInfo = this.$store.state.userInfo;
							let myInfo = res.data.data.member_info;
							if (myInfo) {
								if (myInfo.mobile) {
									this.$store.state.hasbingMobile = true;
									userInfo.mobile =myInfo.mobile;
								}
								userInfo.member_level=myInfo.member_level
								this.login(userInfo);
							}
							setTimeout(()=>{
								uni.switchTab({
									url:'/pages/index/index'
								})
							},1000)
						} else if (res.data.code = 40001) {
							this.$api.msg(res.data.message);
						} else {
							this.$api.msg(res.data.message);
							this.logining = false;
						}
						this.loading = true;
					},
					fali: (res) => {
						console.log(res.data);
					}
			
				});
			},
			cancel(){
				uni.switchTab({
					url:'/pages/index/index'
				})
			}
		}
	}
</script>

<style lang="scss">
//授权登录
	.authLogin{
		height: 100vh;
		    display: table;
	    width: 100%;
		.pattern {
	    position: absolute;
	    top: 0;
	    z-index: 1;
	    max-width: 312px;
	    width: 21.66%;
		height: 270px;
			image{
				width: 100%;
				height:100%;
			}
		}
		.left{
			left:0;
		}
		.right{
			right:0;
		}
		.main{
			    display: table-cell;
	    height: 100%;
	    vertical-align: middle;
	    text-align: center;
			.icon{
				image{
					width: 240upx;
					height:240upx;
					border-radius: 40upx;
				}
				margin: 0 auto;
				width: 240upx;
				height:240upx;
			}
			.name{
				font-size:24px;
				margin: 40upx auto;
			}
			.btn{
				width:440upx;
				height:80upx;
				line-height: 80upx;
				text-align: center;
				margin: 40upx auto;
				font-size:34upx;
				color:#fff;
				background-color:blue;
				border-radius: 20upx;
				
			}
		}
	}
	// 
</style>
